package org.afi.mushroomplayer.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

public class DialogToastUtils {

	private static final String TAG = "DialogToastUtils";

	public static Toast toast;

	public static AlertDialog showDialog(Context context, CharSequence title,
			CharSequence msg, CharSequence leftBtn,
			DialogInterface.OnClickListener leftListener,
			CharSequence rightBtn, DialogInterface.OnClickListener rightListener) {
		return showDialog(context, title, msg, leftBtn, leftListener, rightBtn,
				rightListener, null, null, null);
	}
	
	public static AlertDialog showDialog(Context context, CharSequence title,
			CharSequence msg, CharSequence leftBtn,
			DialogInterface.OnClickListener leftListener,
			CharSequence rightBtn, DialogInterface.OnClickListener rightListener,
			View view) {
		return showDialog(context, title, msg, leftBtn, leftListener, rightBtn,
				rightListener, null, null, null, view);
	}

	public static AlertDialog showDialog(Context context, CharSequence title,
			CharSequence msg, CharSequence leftBtn,
			DialogInterface.OnClickListener leftListener,
			CharSequence rightBtn,
			DialogInterface.OnClickListener rightListener,
			CharSequence middleBtn,
			DialogInterface.OnClickListener middleListener,
			DialogInterface.OnCancelListener cancelListener) {
		if (isContextFinished(context)) {
			return null;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(context);

		if (title != null) {
			builder.setTitle(title);
		}
		if (msg != null) {
			builder.setMessage(msg);
		}
		if (leftBtn != null) {
			builder.setPositiveButton(leftBtn, leftListener);
		}
		if (rightBtn != null) {
			builder.setNegativeButton(rightBtn, rightListener);
		}
		if (middleBtn != null) {
			builder.setNeutralButton(middleBtn, middleListener);
		}
		if (cancelListener != null) {
			builder.setOnCancelListener(cancelListener);
		}
		builder.setOnKeyListener(new OnKeyListener() {
			@Override
            public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_SEARCH:
					return true;
				}
				return false;
			}
		});

		try {
			return builder.show();
		} catch (Throwable e) {
			Log.e(TAG, "Failed to show alertdialog.", e);
			return null;
		}
	}
	
	public static AlertDialog showDialog(Context context, CharSequence title,
			CharSequence msg, CharSequence leftBtn,
			DialogInterface.OnClickListener leftListener,
			CharSequence rightBtn,
			DialogInterface.OnClickListener rightListener,
			CharSequence middleBtn,
			DialogInterface.OnClickListener middleListener,
			DialogInterface.OnCancelListener cancelListener, View view) {
		if (isContextFinished(context)) {
			return null;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		
		if (title != null) {
			builder.setTitle(title);
		}
		if (msg != null) {
			builder.setMessage(msg);
		}
		if (leftBtn != null) {
			builder.setPositiveButton(leftBtn, leftListener);
		}
		if (rightBtn != null) {
			builder.setNegativeButton(rightBtn, rightListener);
		}
		if (middleBtn != null) {
			builder.setNeutralButton(middleBtn, middleListener);
		}
		if (cancelListener != null) {
			builder.setOnCancelListener(cancelListener);
		}
		
		if (view != null) {
			builder.setView(view);
		}
		builder.setOnKeyListener(new OnKeyListener() {
			@Override
            public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_SEARCH:
					return true;
				}
				return false;
			}
		});

		try {
			return builder.show();
		} catch (Throwable e) {
			Log.e(TAG, "Failed to show alertdialog.", e);
			return null;
		}
	}

	public static void showToast(Context context, CharSequence message) {
		if (DialogToastUtils.toast != null && !DeviceUtils.isIceCreamSandwich()) {
			DialogToastUtils.toast.cancel();
		}
		DialogToastUtils.toast = Toast.makeText(context, message,
				Toast.LENGTH_SHORT);
		DialogToastUtils.toast.show();
	}

	public static void showToast(Context context, int messageId) {
		if (DialogToastUtils.toast != null && !DeviceUtils.isIceCreamSandwich()) {
			DialogToastUtils.toast.cancel();
		}
		DialogToastUtils.toast = Toast.makeText(context, messageId,
				Toast.LENGTH_SHORT);
		DialogToastUtils.toast.show();
	}

	public static ProgressDialog showProgressDialog(Context context,
			CharSequence title, CharSequence message, boolean indeterminate,
			boolean cancelable) {
		ProgressDialog dialog = new ProgressDialog(context);
		dialog.setTitle(title);
		dialog.setMessage(message);
		dialog.setIndeterminate(indeterminate);
		dialog.setCancelable(cancelable);
		try {
			dialog.show();
		} catch (Exception e) {
			Log.e(TAG, "Show ProgressDialog failed.", e);
		}
		return dialog;
	}

	public static void dismissProgressDialog(ProgressDialog dialog,
			Context context) {
		if (!isContextFinished(context)) {
			if (dialog != null && dialog.isShowing()) {
				try {
					dialog.dismiss();
				} catch (RuntimeException e) {
					Log.e(TAG, "Dismiss ProgressDialog failed.", e);
				}
			}
		}
	}
	
	public static void dismissAlertDialog(AlertDialog dialog,
			Context context) {
		if(!isContextFinished(context)) {
			if (dialog != null && dialog.isShowing()) {
				try {
					dialog.dismiss();
				} catch (RuntimeException e) {
					Log.e(TAG, "Dismiss ProgressDialog failed.", e);
				}
			}
		}
	}

	public static boolean isContextFinished(Context context) {
		return context instanceof Activity
				&& ((Activity) context).isFinishing();
	}
}
